package com.yzt.message.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.yzt.message.R;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.constants.IntentConstant;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.message.GroupInfoBean;
import com.yzt.zhuju.network.entity.user.GroupProfileBase;
import com.yzt.zhuju.network.entity.user.GroupProfileList;
import com.yzt.zhuju.utils.BarUtils;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Group;

/**
 * author : bob
 * date   : 2022/12/8
 * desc   : 创建群聊 - 设置名称
 */
@SuppressLint("SetTextI18n")
public class GroupCreateStep2Activity extends BaseActivity {

    private EditText desc;

    private ArrayList<String> extraList;

    public static void start(Activity activity, ArrayList<String> content, int resultCode) {
        Intent intent = new Intent(activity, GroupCreateStep2Activity.class);
        intent.putExtra(IntentConstant.INTENT_CONTENT, content);
        activity.startActivityForResult(intent, resultCode);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_group_create_step_2;
    }

    @Override
    public void installViews() {
        desc = findViewById(R.id.edit_name_info);
        extraList = getIntent().getStringArrayListExtra(IntentConstant.INTENT_CONTENT);
        mTitleBar.setTitle("创建群聊");
        findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = desc.getText().toString();

                if (name.equals("")) {
                    showToast("请输入群聊名称");
                    return;
                }

                BizLayer.getInstance().getMessageModule().groupChatCreate(name, extraList, new AresSubscriber<GroupInfoBean>() {
                    @Override
                    protected void onSuccess(GroupInfoBean bean) {
                        List<String> list = new ArrayList<>();
                        list.add(bean.getRongGroupId());
                        BizLayer.getInstance().getAccountModule().loadGroupProfileList(list, new AresSubscriber<GroupProfileBase>() {
                            @Override
                            protected void onSuccess(GroupProfileBase userProfileList) {
                                try {
                                    GroupProfileList mGroupInfo = userProfileList.getList().get(0);
                                    String groupInfo = JSON.toJSONString(mGroupInfo.getGroupMembers());
                                    Group group = new Group(bean.getRongGroupId(), name, Uri.parse(groupInfo));
                                    RongIM.getInstance().setMessageAttachedUserInfo(true);
                                    RongIM.getInstance().refreshGroupInfoCache(group);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                showToast("创建成功！");
                                setResult(RESULT_OK);
                                finish();
                            }
                        });
                    }

                    @Override
                    protected void onError(int code, String info) {
                        super.onError(code, info);
                    }

                    @Override
                    protected void onFinallyEnd() {
                        super.onFinallyEnd();
                    }
                });
            }
        });
    }
}

